package com.atom.module.document.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.atom.module.document.R;

/**
 * All rights Reserved, Designed By www.rongdasoft.com
 *
 * @version V1.0
 * @Title: GridLineView
 * @Description:
 * @author: wuyongzheng
 * @date: 2022/3/25
 * @Copyright: 2022/3/25 www.rongdasoft.com Inc. All rights reserved.
 */
public class GridLineView extends View {

    private final Paint mGuideHorizontalLinePaint;
    private final Paint mGuideVerticalLinePaint;

    private int horizontalLineSize;
    private int horizontalLineColor;
    private float horizontalLineWidth;

    private int verticalLineSize;
    private int verticalLineColor;
    private float verticalLineWidth;

    public GridLineView(Context context) {
        this(context, null);
    }

    public GridLineView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    @SuppressLint("Recycle")
    public GridLineView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        float density = getResources().getDisplayMetrics().density;
        // init attr
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.GridLineView);
        horizontalLineWidth = ta.getDimension(R.styleable.GridLineView_horizontalLineWidth, density);
        verticalLineWidth = ta.getDimension(R.styleable.GridLineView_verticalLineWidth, density);
        horizontalLineColor = ta.getColor(R.styleable.GridLineView_horizontalLineColor, Color.GREEN);
        verticalLineColor = ta.getColor(R.styleable.GridLineView_verticalLineColor, Color.GREEN);
        horizontalLineSize = Math.max(ta.getInt(R.styleable.GridLineView_horizontalLineSize, 2), 2);
        verticalLineSize = Math.max(ta.getInt(R.styleable.GridLineView_verticalLineSize, 2), 2);
        ta.recycle();
        // init paint
        mGuideHorizontalLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mGuideHorizontalLinePaint.setColor(horizontalLineColor);
        mGuideHorizontalLinePaint.setStyle(Paint.Style.FILL);
        mGuideHorizontalLinePaint.setStrokeWidth(horizontalLineWidth);

        mGuideVerticalLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mGuideVerticalLinePaint.setColor(verticalLineColor);
        mGuideVerticalLinePaint.setStyle(Paint.Style.FILL);
        mGuideVerticalLinePaint.setStrokeWidth(verticalLineWidth);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawHorizontalLine(canvas);
        drawVerticalLine(canvas);
    }

    private void drawHorizontalLine(Canvas canvas) {
        if (horizontalLineSize <= 0) {
            return;
        }
        // 获取步长
        int origW = getMeasuredWidth();
        int origH = getMeasuredHeight();
        float heightStep = origH / (float) (horizontalLineSize + 1);
        for (int i = 0; i < horizontalLineSize; i++) {
            float currStep = heightStep * (i + 1);
            canvas.drawLine(0, currStep, origW, currStep, mGuideHorizontalLinePaint);
        }
    }

    private void drawVerticalLine(Canvas canvas) {
        if (verticalLineSize <= 0) {
            return;
        }
        // 获取步长
        int origW = getMeasuredWidth();
        int origH = getMeasuredHeight();
        float widthStep = origW / (float) (verticalLineSize + 1);
        for (int i = 0; i < verticalLineSize; i++) {
            float currStep = widthStep * (i + 1);
            canvas.drawLine(currStep, 0, currStep, origH, mGuideVerticalLinePaint);
        }
    }
}
